var prefix = api + "/admin/client";

$(function () {
	var grant_types = dictByType('grant_types')
	var addFlag = permissions.sys_client_add
	var editFlag = permissions.sys_client_edit
	var deleteFlag = permissions.sys_client_del
	//没有新增权限的话删除新增按钮
	if (!addFlag) {
		$('#addBtn').remove()
	}
	var options = {
		url: prefix + "/page",
		updateUrl: "/pages/sysadm/client/clientEdit",
		removeUrl: prefix + "/{id}",
		modalName: "终端管理",
		queryParams: function (params) {
			var data = {
				size: params.limit,
				current: (params.offset / params.limit + 1),
				search: params.search,
			};
			return data
		},
		columns: [
			{
				title: "序号",
				formatter: function (value, row, index) {
					return $.table.serialNumber(index);
				}
			},
			{
				field: 'clientId',
				align: 'center',
				title: '编号'
			},
			{
				field: 'clientSecret',
				align: 'center',
				title: '密钥'
			},
			{
				field: 'scope',
				align: 'center',
				title: '域',
			},
			{
				field: 'autoapprove',
				align: 'center',
				title: '自动放行',
				formatter: function (value, row, index) {
					return value == "true" ? "是" : "否";
				}
			},
			{
				field: 'authorizedGrantTypes',
				align: 'center',
				title: '授权模式',
				formatter: function (value, row, index) {
					return $.table.selectDictLabelArr(grant_types, value);
				}
			},
			{
				field: 'accessTokenValidity',
				align: 'center',
				title: '令牌时效（秒）',
			},
			{
				field: 'refreshTokenValidity',
				align: 'center',
				title: '刷新时效（秒）'
			},
			{
				title: '操作',
				align: 'center',
				width: 200,
				formatter: function (value, row, index) {
					var actions = [];
					if (editFlag) {
						actions.push('<a class="btn btn-success btn-xs " href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
					}
					actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="$.operate.view(\'' + row.id + '\')"><i class="fa fa-search"></i>详细</a>');
					if (deleteFlag) {
						actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="$.operate.remove(\'' + row.clientId + '\')"><i class="fa fa-remove"></i>删除</a>');
					}
					return actions.join('');
				}
			}]
	};
	$.table.init(options);
});